<?php
//NICEMIP.COM [Don't forget the beginner's mind]
//Copyright (c) 2017~2099 http://NICEMIP.COM All rights reserved.
namespace app\admin\controller;
use think\Request;
use mip\AdminBase;
use think\Cache;
class ApiAdminStatistics extends AdminBase
{
    protected $db_statistics;
    
    protected $db_trend;
    
    protected $db_flow;
    
    protected $db_domains;
    
    protected $id_org;
    
    protected $cache_key;
    
    protected $host_list;
    
    function __construct()
    {
        $this->db_statistics = db('Statistics');
        $this->db_trend = db('trend');
        $this->db_flow = db('flow');
        $this->db_domains = db('domains');
        $this->id_org = config('orgid');
        $this->cache_key = 'Statistics';
        $host = $this->db_domains->select();
        foreach($host as $k=>$v){
            $this->host_list[$k] = $v['domain'];
        }
        unset($host);
    }
    
    public function index() {
        print_R($_GET);
    }
    
    
    //测试数据
    public function test_data()
    {
        $record = mt_rand(1,9999999);
        $type_id = mt_rand(1,4);
        $time  = time() + (60*60*24 * mt_rand(0,7));  //随机时间
        
        if($this->db_statistics->insert([
            'record'=>$record,
            'type_id'=>$type_id,
            'time'=>$time,
            'id_org'=>$this->id_org,
        ])){
            return jsonSuccess('创建成功');
        };
    }
    
    //测试数据添加
    public function add_test_data()
    {
        $i = 100;
        while($i > 0){
            $this->test_data();
            $i--;
        }
        
    }
    
    //测试流量数据
    public function test_trend_data()
    {
        $time  = time() - (mt_rand(60*60*24,60*60*24*7));  //随机时间
        $browse_mete = mt_rand(1,9999999);
        $type_arr = ['pv','uv'];
        $type =  $type_arr[rand(0,1)];
        
        if($this->db_trend->insert([
            'id_org'=>$this->id_org,
            'trend_time'=>$time,
            'browse_mete'=>$browse_mete,
            'trend_type'=>$type,
        ])){
            return jsonSuccess('创建成功');
        }
        
    }
    
    
    //测试数据添加
    public function add_test_trend_data()
    {
        $i = 100;
        
        while($i > 0){
            $this->test_trend_data();
            $i--;
        }
        
    }
    
    //测试数据
    public function test_flow()
    {
        $flow_visitor = md5(time().rand(10,999999));
        $flow_time = time()+3600*rand(1,24);
        $flow_jump_count = rand(1,9999999);
        $flow_host = $this->host_list[rand(0,1)];
        
        $this->db_flow->insert([
            'flow_visitor'=>$flow_visitor,
            'flow_time'=>$flow_time,
            'flow_jump_count'=>$flow_jump_count,
            'flow_host'=>$flow_host,
        ]);
        
        
    }
    
    //测试数据添加
    public function add_test_flow()
    {
        $i = 100;
        
        while($i > 0){
            $this->test_flow();
            $i--;
        }
        
    }
    
    
    
    
    //收录统计
    public function getStatisticsList($min_time='',$max_time='')
    {
        
        $week = $this->get_week(time());
        // foreach($week as $key=>$val){
        //     $week_date[] = $val['date'];
        // }
        
        $min_time = strtotime($min_time) ?: strtotime($week[0]);
        $max_time = strtotime($max_time) ?: strtotime($week[6]);
        
        
        
        
        // Cache::clear();
        // Cache::set($this->cache_key,$list,3600);
        
        if(Cache::has($this->cache_key) === true){
            
            $list = Cache::get($this->cache_key);
        }else{
            $list = $this->db_statistics
            ->alias('s')
            ->join('Statistics_type t','s.type_id = t.type_id')
            ->order('s.s_id')
            ->where('s.time >='.$min_time)
            ->where('s.time <='.$max_time)
            ->where('s.id_org ='.$this->id_org)
            ->fetchSql(false)
            ->select();
            if(!empty($list)){
                Cache::set($this->cache_key,$list,3600);
            }else{
                $return['time'][]         = 0;
                $return['baidu'][]         = 0;
                $return['sanliuning'][]    = 0;
                $return['sougou'][]        = 0;
                $return['google'][]        = 0;
                return jsonSuccess($return);
            }
        }
        
        
        
        
        if($list){
            
            $return_arr = [];
            $data_list = Cache::get($this->cache_key);
            $date_list_count = count($data_list);
            $type_arr = ['1,2,3,4'];
            
            
            foreach($data_list  as $k=>$v){
                $data_list[$k]['time'] = date('Y-m-d',$v['time']);
            }
            
            //取去时间 进行去重
            foreach($data_list as $key=>$val){
                $arr_day[] = $val['time'];
            }
            
            $arr_day = array_unique($arr_day);
            sort($arr_day);
            
            
            for($i=0;$i <= $date_list_count;$i++){
                if(in_array($data_list[$i]['time'],$arr_day)){
                    $return_arr[ $data_list[$i]['time'] ]['time']   = $data_list[$i]['time'];
                    // $return_arr[ date('Y-m-d',$data_list[$i]['time']) ]['word']   += $data_list[$i]['word'];
                    
                    if($data_list[$i]['type_id'] == '1'){
                        $return_arr[ $data_list[$i]['time'] ]['baidu'] += $data_list[$i]['record'];
                    }elseif($data_list[$i]['type_id'] == '2'){
                        $return_arr[ $data_list[$i]['time'] ]['sanliuning'] += $data_list[$i]['record'];
                    }elseif($data_list[$i]['type_id'] == '3'){
                        $return_arr[ $data_list[$i]['time'] ]['sougou'] += $data_list[$i]['record'];
                    }elseif($data_list[$i]['type_id'] == '4'){
                        $return_arr[ $data_list[$i]['time'] ]['google'] += $data_list[$i]['record'];
                    }
                    
                    
                }
                
            }
            
            
            foreach($return_arr as $key=>$val)
            {
                $return['time'][]          = $val['time'] ?:0;
                $return['baidu'][]         = $val['baidu'] ?:0;
                $return['sanliuning'][]    = $val['sanliuning'] ?:0;
                $return['sougou'][]        = $val['sougou'] ?:0;
                $return['google'][]        = $val['google'] ?:0;
                
            }
            
            sort($return['time']);
            
            return jsonSuccess($return);
            
        }else{
            
            return jsonError('参数错误');
        }
        
    }
    
    
    
    //获取流量列表
    public function getTrendlist($type='pv',$Compared_time='onday'){
        
        
        
        switch ($Compared_time) {
            case 'onday':
                //对比一天
                $node = array();
                
                for($i=0;$i<25;$i++){
                    if($i % 4 == 0){
                        $node_time_arr[]  = $i;
                    }
                }
                
                $onday     = strtotime(date('Y-m-d',time())) - 3600*24;   //前一天
                $nowtime   = strtotime(date('Y-m-d',time()));             //今天
                $tomorrow  = strtotime(date('Y-m-d',time())) + 3600*24;  //明天
                
                //昨天流量查询
                if($type == 'uv'){
                    
                    $onday_flow_list = $this->db_flow
                    ->alias('f')
                    ->join('domains d','f.flow_host = d.domain')
                    ->where("f.flow_time >=$onday")
                    ->where("f.flow_time <$nowtime")
                    ->where('d.id_org = '.$this->id_org)
                    ->group('f.flow_visitor')
                    ->order('f.flow_time asc')
                    ->fetchsql(false)
                    ->select();
                    //处理上一天的数据
                    foreach($onday_flow_list as $k=>$v){
                        $return_node_time  = $this->node_time(date('H',$v['flow_time']));
                        
                        if(in_array($return_node_time,$node_time_arr)){
                            $trend_return[$return_node_time]['flow_time']   = $return_node_time;
                            if(!empty($v['flow_visitor'])){
                                $trend_return[$return_node_time]['onday']   +=1;
                            }
                            
                        }
                        
                    }
                    
                    
                }else{
                    
                    $onday_flow_list  = $this->db_flow
                    ->alias('f')
                    ->join('domains d','f.flow_host = d.domain')
                    ->where("f.flow_time >=$onday")
                    ->where("f.flow_time <$nowtime")
                    ->where('d.id_org = '.$this->id_org)
                    ->order('f.flow_time asc')
                    ->fetchsql(false)
                    ->select();
                    //处理上一天的数据
                    foreach($onday_flow_list as $k=>$v){
                        $return_node_time  = $this->node_time(date('H',$v['flow_time']));
                        
                        if(in_array($return_node_time,$node_time_arr)){
                            $trend_return[$return_node_time]['flow_time']   = $return_node_time;
                            $trend_return[$return_node_time]['onday']       += $v['flow_jump_count'];
                        }
                        
                    }
                    
                    
                }
                
                
                if($trend_return){
                    unset($return_node_time);
                }
                
                //今天流量查询
                if($type == 'uv'){
                    $nowtime_flow_list  = $this->db_flow
                    ->alias('f')
                    ->join('domains d','f.flow_host = d.domain')
                    ->where("f.flow_time >=$nowtime")
                    ->where("f.flow_time <$tomorrow")
                    ->where('d.id_org = '.$this->id_org)
                    ->group('f.flow_visitor')
                    ->fetchsql(false)
                    ->order('f.flow_time asc')
                    ->select();
                    
                    foreach($nowtime_flow_list as $key =>$val){
                        $now_data_time = $this->node_time(date('H',$val['flow_time']));
                        if(in_array($now_data_time,$node_time_arr)){
                            $trend_return[$now_data_time]['flow_time']   = $now_data_time;
                            if(!empty($val['flow_visitor'])){
                                $trend_return[$now_data_time]['now']     += 1;
                            }
                        }
                    }
                    
                }else{
                    $nowtime_flow_list  = $this->db_flow
                    ->alias('f')
                    ->join('domains d','f.flow_host = d.domain')
                    ->where("f.flow_time >=$nowtime")
                    ->where("f.flow_time <$tomorrow")
                    ->where('d.id_org = '.$this->id_org)
                    ->fetchsql(false)
                    ->order('f.flow_time asc')
                    ->select();
                    
                    foreach($nowtime_flow_list as $key =>$val){
                        $now_data_time = $this->node_time(date('H',$val['flow_time']));
                        if(in_array($now_data_time,$node_time_arr)){
                            $trend_return[$now_data_time]['flow_time']   = $now_data_time;
                            $trend_return[$now_data_time]['now']        += $val['flow_jump_count'];
                        }
                    }
                    
                }
                // print_R($trend_return);exit;
                if(!empty($trend_return)){
                    foreach($trend_return as $kk=>$vv){
                        $trend_return['flow_time'][]     = $vv['flow_time']?:0;
                        if(!empty($vv['onday'])){
                            $trend_return['onday'][]  = $vv['onday'];
                        }else{
                            $trend_return['onday'][]  = 0;
                        }
                        $trend_return['now'][]        = $vv['now']?:0;
                        unset($trend_return[$kk]);
                    }
                    
                    // sort($trend_return['flow_time']);
                    
                }else{
                    $trend_return['flow_time']  = date('H',time());
                    $trend_return['onday']       = 0;
                    $trend_return['now']         = 0;
                }
                
                
                if($trend_return){
                    return jsonSuccess($trend_return);
                }
                
                break;
                
            case 'onweek':
                
                for($i=0;$i<7;$i++){
                    $node_time_arr[] = $i+1;
                }
                
                $yester_week = $this->get_week(time() - 3600*24*7);   //上一周
                $min_yester_day = strtotime($yester_week[0]);
                $max_yester_day = strtotime($yester_week[6]);
                $now_week    = $this->get_week(time());   //本周
                $min_now_week = strtotime($now_week[0]);
                $max_now_week = strtotime($now_week[6]);
                
                $trend_return = array();
                
                
                if($type == 'uv'){
                    
                    $on_flow_list  = $this->db_flow
                    ->alias('f')
                    ->join('domains d','f.flow_host = d.domain')
                    ->where("f.flow_time >=$min_yester_day")
                    ->where("f.flow_time <=$max_yester_day")
                    ->where('d.id_org = '.$this->id_org)
                    ->group('f.flow_visitor')
                    ->order('f.flow_time asc')
                    ->fetchSql(false)
                    ->select(); //上一周数据
                    
                    $now_flow_list  = $this->db_flow
                    ->alias('f')
                    ->join('domains d','f.flow_host = d.domain')
                    ->where("f.flow_time >=$min_now_week")
                    ->where("f.flow_time <=$max_now_week")
                    ->where('d.id_org = '.$this->id_org)
                    ->group('f.flow_visitor')
                    ->order('f.flow_time asc')
                    ->fetchSql(false)
                    ->select(); //本周数据
                    
                    foreach($on_flow_list as $key=>$val)
                    {
                        $now_data_time = $this->node_time(date('w',$val['flow_time']),'week');
                        $on_trend_list[$key]['flow_time'] = date('w',$val['flow_time']);
                        
                        if(in_array($now_data_time,$node_time_arr)){
                            $trend_return[$now_data_time]['flow_time']     = $now_data_time;
                            if(!empty($val['flow_jump_count'])) {
                                $trend_return[$now_data_time]['onday']     +=1;
                            }
                        }
                    }
                    
                    
                    if(!empty($trend_return)) unset($on_flow_list);
                    
                    foreach($node_time_arr as $vvv){
                        
                        foreach($now_flow_list as $k=>$v)
                        {
                            $now_data_time = $this->node_time(date('w',$v['flow_time']),'week');
                            
                            if($now_data_time == $vvv){
                                $trend_return[$vvv]['flow_time']   = $now_data_time;
                                $trend_return[$vvv]['now']     +=  !empty($v['flow_jump_count']) ?  1:0;
                            }else{
                                $trend_return[$vvv]['flow_time']   = $vvv;
                                $trend_return[$vvv]['now']          = 1;
                            }
                        }
                        
                    }
                    
                    
                    if(!empty($trend_return)) unset($now_trend_list);
                    
                    foreach($trend_return as $kk=>$vv){
                        $trend_return['flow_time'][]  = $vv['flow_time'];
                        $trend_return['onday'][]      = $vv['onday'] ?: 0;
                        $trend_return['now'][]        = $vv['now'] ?: 0;
                        unset($trend_return[$kk]);
                    }
                    
                }else{
                    $on_flow_list  = $this->db_flow
                    ->alias('f')
                    ->join('domains d','f.flow_host = d.domain')
                    ->where("f.flow_time >=$min_yester_day")
                    ->where("f.flow_time <=$max_yester_day")
                    ->where('d.id_org = '.$this->id_org)
                    ->order('f.flow_time asc')
                    ->fetchSql(false)
                    ->select(); //上一周数据
                    
                    $now_flow_list  = $this->db_flow
                    ->alias('f')
                    ->join('domains d','f.flow_host = d.domain')
                    ->where("f.flow_time >=$min_now_week")
                    ->where("f.flow_time <=$max_now_week")
                    ->where('d.id_org = '.$this->id_org)
                    ->order('f.flow_time asc')
                    ->fetchSql(false)
                    ->select(); //本周数据
                    
                    foreach($on_flow_list as $key=>$val)
                    {
                        $now_data_time = $this->node_time(date('w',$val['flow_time']),'week');
                        $on_trend_list[$key]['flow_time'] = date('w',$val['flow_time']);
                        
                        if(in_array($now_data_time,$node_time_arr)){
                            $trend_return[$now_data_time]['flow_time']     = $now_data_time;
                            
                            $trend_return[$now_data_time]['onday']        += $val['flow_jump_count'];
                            
                        }
                    }
                    
                    if(!empty($trend_return)) unset($on_flow_list);
                    foreach($node_time_arr as $vvv){
                        
                        foreach($now_flow_list as $k=>$v)
                        {
                            $now_data_time = $this->node_time(date('w',$v['flow_time']),'week');
                            if($now_data_time == $vvv){
                                $trend_return[$vvv]['flow_time']   = $now_data_time;
                                $trend_return[$vvv]['now']     += $v['flow_jump_count'];
                            }else{
                                $trend_return[$vvv]['flow_time']   = $vvv;
                                $trend_return[$vvv]['now']          = 0;
                            }
                            
                        }
                        
                    }
                    
                    
                    if(!empty($trend_return)) unset($now_trend_list);
                    
                    foreach($trend_return as $kk=>$vv){
                        $trend_return['flow_time'][]  = $vv['flow_time'];
                        $trend_return['onday'][]      = $vv['onday'] ?: 0;
                        $trend_return['now'][]        = $vv['now'] ?: 0;
                        
                        unset($trend_return[$kk]);
                    }
                }
                
                
                $trend_return['title'] = ['今周','上一周'];
                
                if(!empty($trend_return))sort($trend_return['flow_time']);
                
                
                if($trend_return){
                    return jsonSuccess($trend_return);
                }
                
                break;
        }
        
        
    }
    
    
    //时间节点对比一天
    public function node_time($str,$type='day'){
        
        switch ($type) {
            case 'day':
                
                for($i=0;$i<25;$i++){
                    if($i % 4 == 0){
                        $node_time_arr[]  = $i;
                    }
                }
                
                for($j=0;$j<count($node_time_arr);$j++){
                    
                    $Compared = $node_time_arr[$j+1];
                    $now = $node_time_arr[$j];
                    
                    if($str >= $now and $str < $Compared){
                        return $now;
                        unset($str);
                    }
                }
                
                break;
                
            case 'week':
                
                for($i=0;$i<7;$i++){
                    $node_time_arr[] = $i+1;
                }
                
                for($j=0;$j<count($node_time_arr);$j++){
                    
                    $now = $node_time_arr[$j];
                    
                    if($str  == $now){
                        
                        return $now;
                        unset($str);
                    }
                }
                
                break;
        }
        
        
    }
    
    
    //流量统计
    public function flow_list(){
        $now_time = strtotime(date('Y-m-d',time()));
        $yesterday = strtotime(date('Y-m-d',time())) - 3600*24;
        $return_arr  = [];
        
        
        $now_time_pv_sum = $this->db_trend->where('trend_time >='.$now_time)
        ->where('trend_time <'.($now_time + 3600*24))
        ->where("trend_type='pv'")
        ->fetchSql(false)
        ->field('sum(browse_mete) as sum_browse_mete')
        ->select();
        $return_arr['now']['pv_sum'] = $now_time_pv_sum[0]['sum_browse_mete']?:0;
        
        
        $now_time_uv_sum = $this->db_trend->where('trend_time >='.$now_time)
        ->where('trend_time <'.($now_time + 3600*24))
        ->where("trend_type='uv'")
        ->fetchSql(false)
        ->field('sum(browse_mete) as sum_browse_mete')
        ->select();
        $return_arr['now']['uv_sum'] = $now_time_uv_sum[0]['sum_browse_mete']?:0;
        
        $now_time_visitor_count = $this->db_flow->where('flow_time >='.$now_time)
        ->where('flow_time <'.($now_time + 3600*24))
        ->fetchSql(false)
        ->field('count(flow_visitor) as count_flow_visitor')
        ->select();
        $return_arr['now']['visitor'] = $now_time_visitor_count[0]['count_flow_visitor']?:0;
        
        
        
        
        $yesterday_pv_sum = $this->db_trend->where('trend_time >='.$yesterday)
        ->where('trend_time <'.($yesterday + 3600*24))
        ->where("trend_type='pv'")
        ->field('sum(browse_mete) as sum_browse_mete')
        ->select();
        $return_arr['yesterday']['pv_sum'] = $yesterday_pv_sum[0]['sum_browse_mete'] ?:0;
        
        $yesterday_pv_sum = $this->db_trend->where('trend_time >='.$yesterday)
        ->where('trend_time <'.($yesterday + 3600*24))
        ->where("trend_type='uv'")
        ->field('sum(browse_mete) as sum_browse_mete')
        ->select();
        
        $return_arr['yesterday']['uv_sum'] = $yesterday_pv_sum[0]['sum_browse_mete']?:0;
        
        $yesterday_visitor_count = $this->db_flow->where('flow_time >='.$yesterday)
        ->where('flow_time <'.($yesterday + 3600*24))
        ->fetchSql(false)
        ->field('count(flow_visitor) as count_flow_visitor')
        ->select();
        
        $return_arr['yesterday']['visitor'] = $yesterday_visitor_count[0]['count_flow_visitor']?:0;
        
        
        // foreach($return_arr as $key=>$value){
        //     $return_arr['pv'] + $value['browse_mete'];
        
        // }
        
        
        return jsonSuccess($return_arr) ?: jsonError('数据有问题');
        
        
    }
    
    
    
    
    
    /**
     * 获取一周日期
     * @param $time 时间戳
     * @param $format 转换格式
     */
    public function get_week($time, $format = "Y-m-d") {
        $week = date('w',$time);
        $weekname=array('星期一','星期二','星期三','星期四','星期五','星期六','星期日');
        //星期日排到末位
        if(empty($week)){
            $week=7;
        }
        for($i=0;$i<= 6;$i++){
            $data[] = date($format,strtotime( '+'. $i + 1 - $week .' days',$time));
            // $data[$i]['week'] = $weekname[$i];
        }
        
        return $data;
    }
    
    
    /**
     *   请求统计
     *   $user_last_time 用户最后浏览时间
     *   $now_time 现在时间
     */
    public function timing_user()
    {
        $url      = $_GET['url'];
        $host     = $_GET['host'];
        $visitor  = md5($_GET['visitor']);
        $time     = time();
        
        $now = $this->db_flow->where("flow_visitor = '$visitor'")->fetchSql(false)->find();
        
        if(!empty($now)){
            
            if(md5($now['flow_host']) == md5($host)){
                //就是同一个页面修改
                $data = [
                    'flow_time'   =>$time,
                    'flow_jump_count'=>$now['flow_jump_count'] + 1,
                ];
                
                if( $this->db_flow->where('flow_id='.$now['flow_id'])->update($data) )
                {
                    return '更新成功';
                }
                
            }else{
                exit;
                if($this->db_flow->insert([
                    'flow_visitor'    => $visitor,
                    'flow_time'       => $time,
                    'flow_jump_count' => 1,
                    'flow_host'       => $host,
                ])){
                    return '新增成功';
                }
            }
            
        }else{
            
            if($this->db_flow->insert([
                'flow_visitor'    => $visitor,
                'flow_time'       => $time,
                'flow_jump_count' => 1,
                'flow_host'       => $host,
            ])){
                
                return '新增成功';
                
            }
            
        }
        
    }
    
    
    /**
     * 定时写入统计表
     */
    public function timing_trend(){
        $now_time = strtotime(date('Y-m-d'),time());
        $flow_list  = $this->db_flow
        ->where('flow_time >='.$now_time)
        ->where('flow_time <'.($now_time+3600*24))
        ->group('flow_visitor')
        // ->field('count(flow_jump_count)')
        ->select();    //当天时间的数据
        
        
        for($i=0;$i<count($flow_list);$i++){
            if($flow_list[$i]['flow_visitor'] != $flow_visitor[$i+1]['flow_visitor']  and in_array($flow_list[$i]['flow_host'],$this->host_list)     ){
                $uv_list['browse_mete'] +=1;
                $flow_host = $flow_list[$i]['flow_host'] ?:0;
                $uv_list['trend_type'] = 'uv';
                $uv_list['id_org'] = $this->db_domains->where("domain='$flow_host'")->value('id_org');
                $uv_list['trend_time'] = time();
                $host = $flow_list[$i]['flow_host'];
                $uv_return[ $host ] = $uv_list;
                unset($host);
            }
        }
        sort($uv_return);
        
        
        $pv_flow_list = $this->db_flow
        ->where('flow_time >='.$now_time)
        ->where('flow_time <'.($now_time+3600*24))
        ->field('sum(flow_jump_count) as jump_count,flow_time,flow_host')
        ->group('flow_host')
        ->fetchSql(false)
        ->select();
        foreach($pv_flow_list as $key=>$val){
            $pv_return[$key]['browse_mete'] = $val['jump_count'] ?:0;
            $pv_return[$key]['trend_type'] = 'pv';
            $host = $val['flow_host'];
            $pv_return[$key]['id_org'] = $this->db_domains->where("domain='$host'")->fetchSql(false)->value('id_org');
            $pv_return[$key]['trend_time']  = $val['flow_time'];
        }
        
        $return = array_merge($uv_return,$pv_return);
        
        
        if($this->db_trend->insertAll($return))
        {
            
            return jsonSuccess('插入成功');
        }
        
        
        
    }
    
    
    //百度请求收录
    public function statistics_baidu($site)
    {
        // $site = 'www.iisp.com';
        $bai_url = 'https://www.baidu.com/s?wd=' . urlencode('site:' . $site);  //百度的
        $header = array(
            'Upgrade-Insecure-Requests:1',
            'User-Agent:' . $_SERVER['HTTP_USER_AGENT']
        );
        
        // if(Cache::has('baidu')){
        //     $content = Cache::get('baidu');
        // }else{
        //      $curl_content = $this->crul($bai_url,$header);
        //      Cache::set('baidu',$curl_content);
        //      $content  = Cache::get('baidu');
        // }
        
        $content = $this->crul($bai_url,$header);
        
        
        // echo $_SERVER['HTTP_USER_AGENT'];exit;
        if (preg_match_all('/(<div[^>]*op_site_domain [^>]*>[\s\S]*<\/div>)[\s]*<div class="op_site_main c-gap-top-small">/i', $content, $sub_content_matches)) {
            $content = str_replace(array(" ", "\t", "\n", "\r"), '', strip_tags($sub_content_matches[1][0]));
        }
        
        
        $pattern = '/<b>找到相关结果数约((\d*\,?)*)个<\/b>/i';
        $match = preg_match_all($pattern, $content, $matches);
        
        
        if (!$match) {
            $pattern = '/该网站共有((\d*\,?)*)个网页被百度收录/i';
            $match = preg_match_all($pattern, $content, $matches);
            $record = str_replace(',','',(string)$matches[1][0]);
            $record = intval($record);
            return $record;
            
        }
        // if($record){
        
        // }
        
    }
    
    //搜狗收录请求
    public function statistics_sougou($site)
    {
        // $site = 'www.iisp.com';
        $sougou_url = 'https://www.sogou.com/web?query=' . urlencode('site:' . $site);  //搜狗的
        
        $header = array(
            'Upgrade-Insecure-Requests:1',
            'User-Agent:' . $_SERVER['HTTP_USER_AGENT']
        );
        $content = $this->crul($sougou_url,$header);
        
        // if(Cache::has('sougou')){
        //     $content = Cache::get('sougou');
        // }else{
        //      $curl_content = $this->crul($sougou_url,$header);
        //      Cache::set('sougou',$curl_content);
        //      $content  = Cache::get('sougou');
        // }
        
        
        
        if (preg_match_all('/<p class="sr-num">(.*?)<\/p>/', $content, $sub_content_matches)) {
            
            $content = str_replace(array(" ", "\t", "\n", "\r"), '', strip_tags($sub_content_matches[1][0]));
        }
        
        
        $pattern = '/找到约((\d*\,?)*)条结果/i';
        $match = preg_match_all($pattern, $content, $matches);
        
        if($match){
            $record = str_replace(',','',(string)$matches[1][0]);
            $record = intval($record);
            return $record;
            
        }
        
    }
    
    public function statistics_360($site){
        // $site = 'www.iisp.com';
        $san_url = 'https://www.so.com/s?q=' . urlencode('site:' . $site);  //360的
        
        $header = array(
            'Upgrade-Insecure-Requests:1',
            'User-Agent:' . $_SERVER['HTTP_USER_AGENT']
        );
        
        $content = $this->crul($san_url,$header);
        
        
        if (preg_match_all('/<p class="info">(.*?)<\/p>/', $content, $sub_content_matches)) {
            
            $content = str_replace(array(" ", "\t", "\n", "\r"), '', strip_tags($sub_content_matches[1][1]));
        }
        
        
        $pattern = '/该网站约((\d*\,?)*)个网页/i';
        $match = preg_match_all($pattern, $content, $matches);
        if($match){
            $record = str_replace(',','',(string)$matches[1][0]);
            $record = intval($record);
            return $record;
            
        }
        
        
    }
    
    
    //定时收录
    public function timing_statistics(){
        $domains_list = $this->db_domains->select();
        foreach($domains_list as $key=>$val){
            $data = [];
            $baidu_statistics  = $this->statistics_baidu($val['domain']);
            if($baidu_statistics){
                $data[] = [
                    'type_id' => 1,   //百度
                    'id_org'  => $val['id_org'],
                    'record'  =>$baidu_statistics ?:0,
                    'time'    =>time(),
                ];
            }
            
            $sougou_statistics = $this->statistics_sougou($val['domain']);
            if($sougou_statistics){
                $data[] = [
                    'type_id' => 3,     //搜狗
                    'id_org'  => $val['id_org'],
                    'record'  =>$sougou_statistics ?:0,
                    'time'    =>time(),
                ];
            }
            $sanliuling_statistics    = $this->statistics_360($val['domain']);
            if($sanliuling_statistics){
                $data[] = [
                    'type_id' => 2,      //360
                    'id_org'  => $val['id_org'],
                    'record'  =>$sanliuling_statistics ?:0,
                    'time'    =>time(),
                ];
            }
            
            if(!empty($data)){
                $this->db_statistics->insertAll($data);
            }
            
        }
        
    }
    
    
    
    
    
    
    
    public function generateHeader($engine = '', $platform = 'pc', $cookie_file = '') {
        $useragent = $this->getUserAgent($platform);
        $header = array(
            'Upgrade-Insecure-Requests:1',
            'User-Agent:' . $useragent
        );
        if ($cookie_file) {
            $header['cookie_file'] = $cookie_file;
        } else {
            if ($engine) {
                // $cookie = $this->cookies[$engine][array_rand($this->cookies[$engine])];
                $cookie = $this->getCookies($engine, $useragent);
            }
            if ($cookie) {
                $header[] = 'Cookie:' . $cookie;
            }
        }
        return $header;
    }
    
    
    public function crul($url,$header){
        
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $result = curl_exec($ch);
        curl_close($ch);
        
        if($result){
            return $result;
        }
        
    }
    
}